package com.ag.hmk.action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.ag.admin.service.impl.UserServiceImpl;
import com.ag.bean.TbDisposalAdvice;
import com.ag.bean.TbEvent;
import com.ag.bean.TbExpert;
import com.ag.bean.TbUri;
import com.ag.bean.TbUser;
import com.ag.common.AnnexPath;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
import com.ag.hmk.service.impl.WordToHtmlSerImpl;
import com.ag.lzm.dao.TbUriDAO;

/**
 * 
 * @author hmk 查询一条专家信息
 */
public class FindOneExpertAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private HmkUiltServImpl hmkUiltServImpl;
	private WordToHtmlSerImpl wordToHtmlSerImpl;
	@Resource(name = "userServiceImpl")
	private UserServiceImpl userService;
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;

	private String expertID; // 专家ID
	private String findType1;
	private TbExpert expertRst;
	private TbUser user;
	private String eventString;

	/*
	 * 查询一条专家记录（by id）
	 */
	public String findOneExpert() throws Exception {
		TbExpert expert1 = null;
		TbUser user3 = null;
		try {
			String hql = "from TbExpert e left join fetch e.tbType left join fetch e.tbExpertArea "
					+ " left join fetch e.tbUser u left join fetch u.tbUserState  "
					+ " left join fetch u.tbDisposalAdvicesForAdviceUId  da  left join fetch da.tbEvent  where 1=1 ";

			if (expertID != null && expertID.length() > 0) {
				hql += " and e.a='" + IDUtil.PRESERVE + "' and e.expId = '"
						+ expertID + "'";
				List result = null;
				result = hmkUiltServImpl.hqlQuery(hql);

				// 参与事件处置 处理开始
				if (result != null) {
					expert1 = (TbExpert) result.iterator().next();
					Iterator it1 = result.iterator();
					if (it1.hasNext()) {
						String str1 = "";
						TbExpert expert2 = (TbExpert) it1.next();
						TbUser user2 = expert2.getTbUser();
						if (user2 != null) {
							Set<TbDisposalAdvice> daSet = user2
									.getTbDisposalAdvicesForAdviceUId();
							if (!daSet.isEmpty()) {
								str1 = "参与了";
								Iterator<TbDisposalAdvice> it2 = daSet
										.iterator();
								while (it2.hasNext()) {
									TbDisposalAdvice dAdvice = it2.next();
									if (dAdvice != null) {
										TbEvent event1 = dAdvice.getTbEvent(); // 得到参与的事件
										if (event1 != null) {
											str1 += (event1.getEEventname() + "、");
										}
									}
								}
								str1 += "处置；";

							} else {
								str1 = "没有参与事件处置";
							}
						} else { // user2为空
							str1 = "没有参与事件处置";
						}
						this.setEventString(str1);
					}
				}
			} else {
				System.out.println("---id 为空，没能找到记录expert");
			}
			// 参与事件处置 处理结束

			user3 = this.userService.findByExpertID(expertID);
			if (user3 != null) {
				System.out.println("====角色：" + user3.getTbUserRoles());
				System.out.println("====部门：" + user3.getTbUserDepts());
			}

		} catch (Exception e) {
			e.printStackTrace();
			expert1 = null;
			user3 = null;
		}

		this.setExpertRst(expert1);
		this.setUser(user3);
		if (findType1 != null && findType1.equals("1")) {
			// 返回专家详情
			if (expert1 != null) {
				try {
					// 复制头像文件到工程的相对文件夹中
					String myname = expert1.getExpImg();
					System.out.println("nanm:" + myname);
					if (myname != null && myname.length()>0) {
						String Objpath = getServletContext().getRealPath("/")
								+ "hmkuploads/" + myname;
						System.out.println("www:" + Objpath);
						TbUri uri1 = uriDAO
								.findById(AnnexPath.EXPERT_IMG_ANNEX_PATH);
						String srcPath = "";
						if (uri1 != null) {
							srcPath = uri1.getUPath() + myname;
						} else {
							srcPath = Objpath;
						}
						System.err.println("fff:" + srcPath);
						if (wordToHtmlSerImpl.isFileExist(Objpath)) {
							System.out.println("不用复制");
						} else {
							// 调用文件复制服务
							if (wordToHtmlSerImpl.copyFile(srcPath, Objpath)) {
								System.out.println("copy file ok");
							} else {
								System.out.println("copy file error");
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("文件复制出错");
				}

			}
			// 返回到专家详情
			return "findOneExpertOK";

		} else if (findType1 != null && findType1.equals("2")) {
			// 返回到事件关联的专家详情
			return "gotoeventExpertDetail";
		} else {
			System.out.println("返回到编辑" + findType1);
			// 返回到专家编辑
			return "gotoEditExpert";
		}

	}

	public TbExpert getExpertRst() {
		return expertRst;
	}

	public void setExpertRst(TbExpert expertRst) {
		this.expertRst = expertRst;
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

	public void setExpertID(String expertID) {
		this.expertID = expertID;
	}

	public void setFindType1(String findType1) {
		this.findType1 = findType1;
	}

	public String getEventString() {
		return eventString;
	}

	public void setEventString(String eventString) {
		this.eventString = eventString;
	}

	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	public void setWordToHtmlSerImpl(WordToHtmlSerImpl wordToHtmlSerImpl) {
		this.wordToHtmlSerImpl = wordToHtmlSerImpl;
	}
}
